package com.zone.vehicle.superApps.view.appmanager.tab;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatButton;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.tamsiree.rxui.view.RxProgressBar;
import com.zone.sdk.module.appstore.entity.AppEntity;
import com.zone.vehicle.superApps.R;
import com.zone.vehicle.superApps.databinding.FragmentAppManagerUpdateBinding;
import com.zone.vehicle.superApps.databinding.FragmentAppManagerUpdateItemBinding;
import com.zone.vehicle.superApps.enums.AppTypeEnum;
import com.zone.vehicle.superApps.model.AppManagerViewModel;
import com.zone.vehicle.superApps.model.ViewModelFactory;
import com.zone.vehicle.superApps.view.common.HandledFragment;

import org.apache.commons.lang3.ObjectUtils;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * A fragment representing a list of Items.
 */
public class AppUpdateListFragment extends HandledFragment {

    private FragmentAppManagerUpdateBinding binding;

    private UpdateAppAdapter updateAppAdapter;

    private AppManagerViewModel viewModel;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentAppManagerUpdateBinding.inflate(inflater);
        viewModel = new ViewModelProvider(requireActivity(), new ViewModelFactory()).get(AppManagerViewModel.class);
        initView();
        return binding.getRoot();
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    private void initView(){
        updateAppAdapter = new UpdateAppAdapter(new ArrayList<>());
        binding.updateList.setAdapter(updateAppAdapter);
        binding.updateList.setLayoutManager(new GridLayoutManager(getContext(), 2));
        viewModel.getNeedUpdateApp().observe(getViewLifecycleOwner(), new Observer<List<AppEntity>>() {
            @Override
            public void onChanged(List<AppEntity> appEntities) {

            }
        });
        viewModel.getActivityApp().observe(getViewLifecycleOwner(), new Observer<List<AppEntity>>() {
            @Override
            public void onChanged(List<AppEntity> appEntities) {
                updateAppAdapter.setData(appEntities);
                updateAppAdapter.notifyDataSetChanged();
            }
        });
        viewModel.getInstalledApp().observe(getViewLifecycleOwner(), new Observer<List<AppEntity>>() {
            @Override
            public void onChanged(List<AppEntity> appEntities) {
//                String buffer = "已安装(" + appEntities.size() + ")";
//                binding.count.setText(buffer);
//                rightAdapter.setData(appEntities);
//                rightAdapter.notifyDataSetChanged();
            }
        });
    }

    private void initData(){
        viewModel.getActivityApp(AppTypeEnum.APP);
        viewModel.getInstalledApp(AppTypeEnum.APP);
    }

    private static class UpdateAppAdapter extends RecyclerView.Adapter<UpdateAppAdapter.ViewHolder>{

        private List<AppEntity> data;

        private FragmentAppManagerUpdateItemBinding binding;

        public UpdateAppAdapter(List<AppEntity> data) {
            this.data = data;
        }

        public void setData(List<AppEntity> data) {
            this.data = data;
        }

        @NotNull
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            binding = FragmentAppManagerUpdateItemBinding.inflate(LayoutInflater.from(parent.getContext()));
            return new ViewHolder(binding.getRoot());
        }

        @Override
        public void onBindViewHolder(@NotNull ViewHolder holder, int position) {
            if (ObjectUtils.isEmpty(data)){
                return;
            }
            AppEntity appEntity = data.get(position);
            binding.name.setText(appEntity.getName());
            //设置图片大小
            RoundedCorners roundedCorners = new RoundedCorners(50);
            RequestOptions options = RequestOptions.bitmapTransform(roundedCorners)
                    .placeholder(new ColorDrawable(Color.GRAY))//设置占位图
//                    .error(R.mipmap.logo_app)//设置异常图片
                    .override(300, 300);//降低内存消耗
            Glide.with(holder.itemView.getContext())
                    .asBitmap()
                    .load(appEntity.getIcon())
                    .apply(options)
                    .into(holder.icon);
            holder.btn_activity.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    holder.btn_activity.setVisibility(View.INVISIBLE);
                    holder.download_progress.setProgress(holder.download_progress.getProgress()+2);
                }
            });
            holder.download_progress.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    holder.btn_activity.setVisibility(View.VISIBLE);
                    holder.download_progress.setProgress(holder.download_progress.getProgress()+2);
                }
            });
        }

        @Override
        public int getItemCount() {
            return data.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {

            private final ImageView icon;
            private final TextView name;
            private final RxProgressBar download_progress;
            private final AppCompatButton btn_activity;

            public ViewHolder(View itemView) {
                super(itemView);
                icon = itemView.findViewById(R.id.icon);
                name = itemView.findViewById(R.id.name);
                download_progress = itemView.findViewById(R.id.download_progress);
                btn_activity = itemView.findViewById(R.id.btn_activity);
            }
        }
    }

}